<?php
/**
 *
 */
class Sx_admin extends CI_Controller {
	private $admin_uname;
	private $admin_upwd;
	private $ynull = TRUE;

	public function index() {
		@session_start();
		//判断是否为登录状态
		if (!isset($_SESSION['sx_admin_user']) || empty($_SESSION['sx_admin_user'])) {
			// 如果未登录，载入登录页面
			$this -> load -> view("admin/admin_login");
		} else {
			//如果为登录状态，直接载入管理首页
			$this -> load -> view("admin/admin_index");
		}

	}

	//接收登录数据
	public function admin_login() {
		@session_start();
		!isset($_SESSION['sx_login_n']) || empty($_SESSION['sx_login_n']) ? $_SESSION['sx_login_n'] = 1 : $_SESSION['sx_login_n'];
		if (!isset($_POST)) {
			print($this -> fengchaoclass -> showhckmsg("","",$this -> lang -> line('hck_admin_login_postnull'), TRUE));
		} else {
			//判断用户名密码
			!isset($_POST['user']) || empty($_POST['user']) ? $this -> ynull = FALSE : $this -> ynull;
			!isset($_POST['pwd']) || empty($_POST['pwd']) ? $this -> ynull = FALSE : $this -> ynull;

			if ($this -> ynull) {
				$y = 0;
				//Y==1用户名包含特殊字符
				$cn = chr(0xa1) . "-" . chr(0xff);
				//是否限定中文？
				$reg = "/^[a-zA-Z0-9($cn)]{1,}$/";
				!preg_match($reg, $_POST['user']) ? $y = 1 : $y;
				//Y==2用户名少于6位
				strlen($_POST['user']) < 4 ? $y = 2 : $y;
				switch ($y) {
					//如果$y为0，则执行登录操作
					case '0' :
						$this -> loginadmin();
						break;
					case '1' :
						print($this -> fengchaoclass -> showhckmsg("","",$this -> lang -> line('hck_admin_login_userpreg'), TRUE));
						break;
					case '2' :
						print($this -> fengchaoclass -> showhckmsg("","",$this -> lang -> line('hck_admin_login_user6'), TRUE));
						break;
					default :
						print($this -> fengchaoclass -> showhckmsg("","",$this -> lang -> line('hck_admin_login_error'), TRUE));
						break;
				}
			} else {
				print($this -> fengchaoclass -> showhckmsg("","",$this -> lang -> line('hck_admin_login_N'), TRUE));
			}

		}
	}

	//登录操作
	private function loginadmin() {
		$this -> admin_uname = htmlspecialchars($_POST['user']);
		$this -> admin_upwd = htmlspecialchars($_POST['pwd']);
		$this -> load -> model('sx_admin_mod');
		$myRet = array();
		if ($this -> sx_admin_mod -> adminlogin($this -> admin_uname, $this -> admin_upwd) == TRUE) {

			$myRet['Msg_error'] = "0";
			$myRet['Msg'] = $this -> lang -> line('hck_admin_login_Y');
			$myRet['MsgTime'] = date("Y-m-d H:i:s");
			print(json_encode($myRet));

		} else {
			$myRet['Msg_error'] = "1";
			$myRet['Msg'] = $this -> lang -> line('hck_admin_login_N');
			$myRet['MsgTime'] = date("Y-m-d H:i:s");
			print(json_encode($myRet));
		}

	}

}
?>